﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Modifyacc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        //post back
        if (!Page.IsPostBack)
        {
            if (Session["user"] != null)
            {
                this.Log_in1.Visible = false;
                this.Log_out1.Visible = true;
                LoadCbb();
                LoadUserInfo();
            }
            else
            {
                this.Log_in1.Visible = true;
                this.Log_out1.Visible = false;
                Response.Redirect("Login.aspx");
            }
        }

    }

    private void LoadUserInfo()
    {
        ProRegister pr = new ProRegister();
        String uid = Session["user"].ToString();
        //load cbb Department
        DataSet ds = pr.LoadUser(uid);
        this.lblUsername.Text = ds.Tables[0].Rows[0]["username"].ToString();
        this.txtFullnameMod.Text = ds.Tables[0].Rows[0]["fullname"].ToString();
        this.txtAddressMod.Text = ds.Tables[0].Rows[0]["address"].ToString();
        this.txtPhoneMod.Text = ds.Tables[0].Rows[0]["phone"].ToString();
        //this.txtBirthdayMod.Text = ds.Tables[0].Rows[0]["birthday"].ToString();
        SetDate();
        DateTime date = DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
        cbbDay.SelectedIndex = date.Day - 1;
        cbbMonth.SelectedIndex = date.Month - 1;
        cbbYear.SelectedIndex = date.Year - Constant.INT_STARTYEAR;

        this.txtEmailMod.Text = ds.Tables[0].Rows[0]["email"].ToString();
        this.cbbDepartment.SelectedIndex = Int32.Parse(ds.Tables[0].Rows[0]["department"].ToString()) - 1;
        this.cbbPosition.SelectedIndex = Int32.Parse(ds.Tables[0].Rows[0]["position"].ToString()) - 1;
        this.imgSignMod.ImageUrl = "images/"+ds.Tables[0].Rows[0]["scan_signature"].ToString();

    }

    private void LoadCbb()
    {
        ProRegister pr = new ProRegister();

        //load cbb Department
        DataSet ds = pr.LoadDepartment();
        this.cbbDepartment.DataSource = ds.Tables[0];
        cbbDepartment.DataTextField = "department";
        cbbDepartment.DataValueField = "id";
        this.cbbDepartment.DataBind();

        //load cbb Position
        DataSet ds1 = pr.LoadPosition();
        this.cbbPosition.DataSource = ds1.Tables[0];
        cbbPosition.DataTextField = "position_name";
        cbbPosition.DataValueField = "id";
        this.cbbPosition.DataBind();
    }

    private void SetDate()
    {
        for (int i = 1; i <= 31; i++)
        {
            this.cbbDay.Items.Add(i.ToString());
        }
        for (int i = 1; i <= 12; i++)
        {
            this.cbbMonth.Items.Add(i.ToString());
        }
        for (int i = Constant.INT_STARTYEAR; i <= Constant.INT_ENDYEAR; i++)
        {
            this.cbbYear.Items.Add(i.ToString());
        }
    }
    protected void cmdSend_Click(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
        {
            ProRegister pr = new ProRegister();
            UserInfo ui = new UserInfo();
            String uid = Session["user"].ToString();
            DataSet ds = pr.LoadUser(uid);
            //check new password or load old password
            if (this.txtPassMod.Text == "")
            {
                ui.StrPassWord = ds.Tables[0].Rows[0]["password"].ToString();
            }
            else
            {
                if (this.txtPassMod.Text != this.txtConPassMod.Text)
                {
                    this.lblError.Text = Constant.MESSAGE_ERROR_DOESNT_MATCH;
                    this.txtConPassMod.Text = "";
                    this.txtPassMod.Text = "";
                    return;
                }
                else
                {
                    ui.StrPassWord = this.txtPassMod.Text;
                }
            }
            //check new signature or laod old signature
            if (this.fupSignMod.HasFile)
            {
                try
                {
                    fupSignMod.SaveAs(Server.MapPath(Constant.STR_MAP_PATH) + ds.Tables[0].Rows[0]["scan_signature"]);
                    ui.ImgSignature = ds.Tables[0].Rows[0]["scan_signature"].ToString();
                }
                catch (Exception ex)
                {
                    this.lblError.Text = Constant.MESSAGE_ERROR_UPLOAD_PIC + ex.Message;
                }
            }
            else
            {
                ui.ImgSignature = ds.Tables[0].Rows[0]["scan_signature"].ToString();
            }
            ui.StrUserName = this.lblUsername.Text;
            ui.StrAddress = this.txtAddressMod.Text;
            //check email
            ui.StrEmail = this.txtEmailMod.Text;
            ui.StrFullName = this.txtFullnameMod.Text;
            //check length of phone
            ui.StrPhone = this.txtPhoneMod.Text;
            //check DoB
            String DoB = this.cbbDay.SelectedValue.ToString() + "/" + this.cbbMonth.SelectedValue.ToString() + "/" + this.cbbYear.SelectedValue.ToString();
            ui.DateBirthday = DateTime.Parse(DoB);
            ui.DepDepartment.Id = Int32.Parse(this.cbbDepartment.SelectedItem.Value);
            ui.DepDepartment.StrDepartment = this.cbbDepartment.SelectedItem.Text;
            ui.PosPosition.Id = Int32.Parse(this.cbbPosition.SelectedItem.Value);
            ui.PosPosition.StrPosition = this.cbbPosition.SelectedItem.Text;
            //ui.StaStatus.Id = Constant.INT_STATUS_DEFAULT_USER;
            ui.StaStatus.StrStatus = Constant.STR_STATUS_DEFAULT_USER_STRING;
            ProModify mod = new ProModify();
            Boolean bResult = mod.EditAccount(ui);
            if (bResult == true)
            {
                Response.Redirect("Default.aspx");
            }
            else this.lblError.Text = Constant.MESSAGE_ERROR_MODIFY;
        }
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        

    }
    
}
